package com.example.compressdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import com.example.compress.NativeLib;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    private Button btTest;
    private Button btTest1;
    private NativeLib nativeLib;
    private final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        nativeLib = new NativeLib();
        setContentView(R.layout.activity_main);
        btTest = findViewById(R.id.btTest);
        btTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String test = "乐敏";

                byte[] data = test.getBytes(StandardCharsets.UTF_8);
                String datainfo = bytes2hex(data);
                Log.e(TAG, "datainfo=" + datainfo);

                byte[] compressbyte = nativeLib.compress(data);
                String compressinfo = bytes2hex(compressbyte);
                Log.e(TAG, "compressinfo=" + compressinfo);


                byte[] test1 = new byte[]{0x78, (byte) 0x9c, 0x7a, (byte) 0xb2, 0x73, (byte) 0xc2, (byte) 0xb3, (byte) 0xa9, (byte) 0xfd, 0x00, 0x01};

                //byte[] uncompressbyte = nativeLib.uncompress(compressbyte);
                byte[] uncompressbyte = nativeLib.uncompress(compressbyte);
                String uncompressinfo = bytes2hex(uncompressbyte);
                String realInfo=new String(uncompressbyte,StandardCharsets.UTF_8);
                Log.e(TAG, "uncompressinfo=" + uncompressinfo);
                Log.e(TAG, "realInfo=" + realInfo);
            }
        });

        btTest1 = findViewById(R.id.btTest1);
        btTest1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String test = "乐敏";

                byte[] data = test.getBytes(StandardCharsets.UTF_8);
                String datainfo = bytes2hex(data);
                Log.e(TAG, "datainfo=" + datainfo);

                byte[] compressbyte = ZLibUtils.compress(data);
                String compressinfo = bytes2hex(compressbyte);
                Log.e(TAG, "compressinfo=" + compressinfo);
            }
        });
    }


    public static String bytes2hex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        String tmp;
        sb.append("[");
        for (byte b : bytes) {
            // 将每个字节与0xFF进行与运算，然后转化为10进制，然后借助于Integer再转化为16进制
            tmp = Integer.toHexString(0xFF & b);
            if (tmp.length() == 1) {
                tmp = "0" + tmp;//只有一位的前面补个0
            }
            sb.append(tmp).append(" ");//每个字节用空格断开
        }
        sb.delete(sb.length() - 1, sb.length());//删除最后一个字节后面对于的空格
        sb.append("]");
        return sb.toString();
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        return super.onKeyLongPress(keyCode, event);
    }
}